﻿
//弹出对话框
window.dialog = function(msg, autoClose, count)
{
    if (autoClose != true) autoClose = false;
    if (!count) count = 3;
    var div = window.document.createElement("DIV");
    div.style.display = "block";
    div.innerHTML = msg;

    var btnText = "确认";
    if (autoClose) btnText = "确认(" + count + ")";
    var btnObj = new Object();
    btnObj[btnText] = function() { $(this).dialog('close'); }


    $(div).dialog({
        modal: true,
        resizable: false,
        title: '提示信息',
        draggable: false,
        buttons: btnObj,
        width: 300,
        height: 150,
        maxHeight: 600,
        maxWidth: 400,
        bgiframe: false,
        open: function()
        {
            $(window.document.body).addClass("ui-modal-true");
        },
        beforeclose: function()
        {
            $(window.document.body).removeClass("ui-modal-true");
        }
    });


    function closew()
    {
        $(div).dialog("close");
    }

    var intervalId;
    if (autoClose)
    {
        window.setTimeout(closew, count * 1000);
        intervalId = window.setInterval(refreshTime, 1000);
    }

    var i = count - 1;
    function refreshTime()
    {
        if (i == 0) window.clearInterval(intervalId);
        var btnText = "确认(" + i + ")";
        var oldObj = $(div).dialog("option", "buttons");
        var newObj = new Object();
        $.each(oldObj, function(i, n)
        {
            newObj[btnText] = n;
        });

        $(div).dialog("option", "buttons", newObj);
        i--;
    }
}


function PicUploadPropertyChanging(fupload, img, hfSrcChanged, hfIsImg)
{
    var imgTmp = null;
    if (imgTmp) imgTmp.removeNode(true);
    imgTmp = document.createElement("img");
    imgTmp.attachEvent("onreadystatechange", isimg);
    imgTmp.attachEvent("onerror", notimg);
    imgTmp.src = fupload.value;
    hfSrcChanged.value = "true";

    function notimg()
    {
        alert("只能上传图片");
        img.src = "";
        hfIsImg.value = "false";
    }
    function isimg()
    {
        img.parentNode.insertAdjacentElement("BeforeEnd", imgTmp);
        imgTmp.style.display = "block";

        var _width = imgTmp.offsetWidth;
        var _height = imgTmp.offsetHeight;

        if (_width != 100 || _height != 140)
        {
            img.parentNode.removeChild(imgTmp);
            imgTmp.style.display = "none";
            alert("图片大小应为100像素*140像素，请重新上传");
            img.src = "";
            hfIsImg.value = "false";
        }
        else
        {
            var pos = path.lastIndexOf(".");
            if (pos == -1 || !(path.substr(pos + 1).toLowerCase() == "gif" || path.substr(pos + 1).toLowerCase() == "jpeg"))
            {

                img.parentNode.removeChild(imgTmp);
                imgTmp.style.display = "none";
                alert("只能上传gif或jpeg格式的图片");
                img.src = "";
                hfIsImg.value = "false";
            }
            else
            {
                img.parentNode.removeChild(imgTmp);
                imgTmp.style.display = "none";
                img.src = path;
                hfIsImg.value = "true";
            }
        }
    }
}